<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script src="jquery-3.4.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
    <style>

        #deptapp {
            padding-top: 10px;
            padding-left: 400px;
            text-align: center;
            /*margin: 0 auto;*/
        }

        th {
            width: 100px;
            outline: black solid;
        }

        td {
            outline: 1px solid black;
        }
    </style>
</head>
<body>

<div id="deptapp">
    <table>
        <tr>
            <th>DEPTNO</th>
            <th>DNAME</th>
            <th>LOC</th>
            <th>ACTION</th>
        </tr>
        <tr v-for="dept in depts">
            <td>{{dept.deptno}}</td>
            <td>{{dept.dname}}</td>
            <td>{{dept.loc}}</td>
            <td><a href="#" @click="deleteById(dept.deptno)">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" @click="updateModal(dept.deptno)">修改</a>
            </td>
        </tr>
    </table>
    <button @click="addModal()">ADDDEPT</button>
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">UPDATEDEPT</h4>
                </div>
                <form @submit="update()">
                    <div class="modal-body" @submit="update()">
                        <input type="hidden" name="deptno" v-model="dept.deptno">
                        <div class="form-group">
                            <label for="dname" class="control-label">DNAME:</label>
                            <input type="text" class="form-control" name="dname" id="dname" v-model="dept.dname">
                        </div>
                        <div class="form-group">
                            <label for="loc" class="control-label">LOC</label>
                            <input class="form-control" id="loc" name="loc" v-model="dept.loc"></input>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <input type="submit"  class="btn btn-primary" value="提交"></input>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="addModallable">ADDDEPT</h4>
                </div>
                <form @submit.prevent="addDept()">
                    <div class="modal-body">
                        <input type="hidden" name="deptno" v-model="dept.deptno">
                        <div class="form-group">
                            <label for="dname" class="control-label">DNAME:</label>
                            <input type="text" class="form-control" name="dname"  v-model="dept.dname">
                        </div>
                        <div class="form-group">
                            <label for="loc" class="control-label">LOC</label>
                            <input class="form-control" name="loc" v-model="dept.loc"></input>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <input type="submit"  class="btn btn-primary" value="提交"></input>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    var app = new Vue({
        el: "#deptapp",
        data: {
            depts: [],
            dept: {deptno:0,dname:'',loc:''},
        },
        methods: {
            deleteById: function (id) {
                $.ajax({
                    url: '/dept' + id,
                    type: 'DELETE',
                    success: function (data) {
                        for (let i = 0; i < app.depts.length; i++) {
                            if (app.depts[i].deptno == id) {
                                app.depts.splice(i, 1);
                            }
                        }
                    }
                })
            },
            updateModal: function (id) {
                $.ajax({
                    url: '/dept' + id,
                    type: 'GET',
                    success: function (data) {
                        app.dept = data;
                        $("#exampleModal").modal('show');
                    }
                })
            },
            update:function (){
                $.ajax({
                    url:'/dept',
                    data:JSON.stringify(app.dept),
                    type:'PUT',
                    contentType:"application/json",
                    success:function (){
                        for (let i = 0; i <app.depts.length; i++) {
                            if (app.depts[i].deptno==id){
                                app.depts[i]=app.dept;
                            }
                        }
                        $("#exampleModal").modal('hide');
                    }
                })
            },
            addModal:function (){
                $("#addModal").modal('show');
            },
            addDept:function (){
                $.ajax({
                    url:'/dept',
                    data:JSON.stringify(app.dept),
                    type:'POST',
                    contentType:"application/json",
                    success:function (data){
                        app.depts.push(data);
                        $("#addModal").modal('hide');
                    }
                })
            }
        }
    });
    $(function () {
        $.ajax({
            url: "/depts",
            type: 'get',
            success: function (data) {
                app.depts = data;
            }
        })
    })
</script>
</body>
</html>